<?php 
namespace Admin\Controller;
use Lib\Controller\AdminBaseController;

class AdminController extends AdminBaseController{
    /**
     * 修改信息
     */
    public function edit_info() {
        //$adminUser = M('Admin');
        $adminUser = M('BbsAdmin','tm_');
        
        if (IS_AJAX) {
            $data['id'] = $_SESSION[C('USER_AUTH_KEY')];
            $data['user_name'] = I('post.username', '');
            //验证用户名格式
//             if (!preg_match('/^[A-Za-z0-9]{6,15}$/', $data['user_name'])) {
//                 $this->tyAjaxReturn(0, '用户名格式不正确！');
//             }
            //验证用户名唯一
            $usernameCount = $adminUser->where("user_name='" . $data['user_name'] . "' and id=" . $data['id'])->count();
            if ($usernameCount >= 1) {
                $this->tyAjaxReturn(0, '用户名已存在！');
            }
//             $data['email'] = I('post.email', '');
//             //验证email格式
//             if (!preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", $data['email'])) {
//                 $this->tyAjaxReturn(0, '邮箱格式不正确！');
//             }
//             //验证email唯一
//             $emailCcount = $adminUser->where("email='" . $data['email'] . "' and id<>" . $data['id'])->count();
//             if ($emailCcount >= 1) {
//                 $this->tyAjaxReturn(0, '邮箱已存在！');
//             }
            $data['original_password'] = I('post.original_password', '');
            //原始密码非空，更新密码
            if (!empty($data['original_password'])) {
                //获取用户密码盐
                $salt = $adminUser->field('user_salt')->find($data['id']);
                //加密密码
                $password = md5($data['original_password'] . $salt['user_salt']);
                //验证原始密码是否正确
                $originalPasswordCcount = $adminUser->where("user_password='" . $password . "' and id=" . $data['id'])->count();
                if ($originalPasswordCcount != 1) {
                    $this->tyAjaxReturn(0, '原始密码错误！');
                }
                $data['password'] = I('post.new_password', '');
                //验证密码格式
                if (!preg_match('/^[A-Za-z0-9\@\!\#\$\%\^\&\*\.\~]{9,16}$/', $data['password'])) {
                    $this->tyAjaxReturn(0, '密码格式不正确！');
                }
                $data['repassword'] = I('post.repassword', '');
                if ($data['password'] != $data['repassword']) {
                    $this->tyAjaxReturn(0, '密码不一致！');
                }
                $data['password'] = md5($data['password'] . $salt['user_salt']);
                unset($data['original_password'], $data['repassword'], $salt);
            } else {
                unset($data['original_password']);
            }
            $data['user_password'] = $data['password'];
            unset($data['password']);
            //$this->ajaxReturn(p($data));
            $result = $adminUser->save($data);
            if ($result !== FALSE) {
                $_SESSION = array();
                
                //删除客户端cookie中保存的sessionid
                if (isset($_COOKIE[session_name()])) {
                    setcookie(session_name(), '', time() - 3600, C('COOKIE_PATH'));
                }
                
                //彻底销毁session
                session_destroy();
                
                S('menuList', NULL);
                S('allMenuId', NULL);
                S('language', NULL);
                S('currency', NULL);
                S('oauths', NULL);
                S('sysConfig', NULL);
                S('jsCacheNum', NULL);
                S('categoryList',NULL);
                $this->tyAjaxReturn(1, L('修改成功'),U(MODULE_NAME . "/Public/login"));
                              
            } else {
                $this->tyAjaxReturn(0, L('修改失败'));
            }
        } else {
            $id = $_SESSION[C('USER_AUTH_KEY')];
            $adminInfo = $adminUser->field('id,user_name')->find($id);
            //p($adminInfo);
            $this->assign('adminInfo', $adminInfo);
            $breadcrumbs = "&nbsp;&nbsp;" . L('修改资料');
            $this->assign('breadcrumbs', $breadcrumbs);
            $this->display();
        }
    }
    
    /**
     * 注销登陆
     */
    public function logout() {
        //清空session数据
        $_SESSION = array();
    
        //删除客户端cookie中保存的sessionid
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time() - 3600, C('COOKIE_PATH'));
        }
    
        //彻底销毁session
        session_destroy();
        
        S('menuList', NULL);
        S('allMenuId', NULL);
        S('language', NULL);
        S('currency', NULL);
        S('oauths', NULL);
        S('sysConfig', NULL);
        S('jsCacheNum', NULL);
        S('categoryList',NULL);
    
        $this->assign("jumpUrl", U(MODULE_NAME . "/Public/login", '', true, false, true, true));
        $this->success('退出成功！');
    }
}
?>